<?php
if(isset($_POST['update_now_tour']))
{
	//print_r($_POST);

	##update order
	
	$orders = array();
	$orders['ID']				= $_POST['update_orderid'];
	$orders['orderStatus'] 		= $_POST['update_orderStatus'];	
	db_update_order($orders);
	
	## update tour
	$tours = array();
	
	$tours['ID']			= $_POST['update_itemID'];	
	$tours['tourID']		= $_POST['update_tour'];	
	$tours['proposeDate']	= $_POST['update_proyr'].'-'.$_POST['update_promo'].'-'.$_POST['update_prody'].' 00:00:00';
	$tours['travelInpartof'] = $_POST['update_trevel_in_party'];
	$tours['typeTour']		 = $_POST['update_tourtype'];
	db_update_tours($tours);
	
	## update payment
	$payments = array();
	$payments['ID']		=	$_POST['update_paymentid'];	
	$payments['paymentStatus']   = $_POST['update_payment'];
	db_update_payment($payments);
	
	## update customer
	$params = array();
	$params['name'] = $_POST['update_customerName'];
	$params['address'] = $_POST['update_customerAddress'];
	$params['email'] = $_POST['update_customerEmail'];
	$params['tel'] = $_POST['update_customerTel'];
	$params['fax'] = $_POST['update_customerFax'];
	$customerID = $_POST['update_customerID'];	
	db_update_customer($customerID,$params);
	
	echo '<span style="color:red;">Update Success!</span>';
}

$view = $_GET['update'];
$url = get_bloginfo('home')."/wp-admin/admin.php?page=tour-setting&action=tour-booked&update=$view";
$order = db_select_data_by_int("orders", "ID", $view);

$customerID	= $order[0]->customerID;
$itemID	= $order[0]->itemID;
$paymentID  = $order[0]->paymentID;

$customers = db_select_data_by_int("customers", "ID", $customerID); 
$tour = db_select_data_by_int("tours", "ID", $itemID); 
$payment = db_select_data_by_int("payments", "ID", $paymentID);

//---------------- Get Tour Price --------------
$tour_price = db_select_all_data("tour_price");

//--------------- Get list tour ----------------
?>
<div class="view-booking-detail">
<form action="" method="post" name="form_update_now_tour">	
	<h1>Tour Booked Infomation</h1>
	<table>
		<tr>
			<td class="booked-title">Booked ID</td>
			<td class="booked-item"><span>
			<?php echo $order[0]->ID;?>
			<input type="hidden" name="update_itemID" value="<?php echo $itemID;?>">
			<input type="hidden" name="update_orderid" value="<?php echo $order[0]->ID;?>">
			<input type="hidden" name="update_paymentid" value="<?php echo $paymentID;?>">
			</span></td>
			
			<td class="booked-title">Date</td>
			<td class="booked-item"><span><?php echo $order[0]->date;?></span></td>
		</tr>
		<tr>
			<td class="booked-title">Payment Status</td>
			<td class="booked-item"><span>
			<select class="select_size" name="update_payment">
			<?php
			$list_payment = db_get_list_payment_status();			
			foreach ($list_payment as $key=>$value) 
			{
				if($payment[0]->paymentStatus == $key)
				{
				?>
					<option selected="selected" value="<?php echo $key?>"><?php echo $value;?></option>
				<?php 	
				}
				else 
				{
				?>
					<option value="<?php echo $key?>"><?php echo $value;?></option>
				<?php	
				}
			} 
			?>
			</select>
			</span></td>
			<td class="booked-title">Order Status</td>
			<td class="booked-item"><span>
			<select class="select_size" name="update_orderStatus">
			<?php //echo $order[0]->orderStatus;?>
			<?php
			//$list_payment = db_get_list_payment_status();			
			foreach ($list_payment as $key=>$value) 
			{
				if($order[0]->orderStatus== $key)
				{
				?>
					<option selected="selected" value="<?php echo $key?>"><?php echo $value;?></option>
				<?php 	
				}
				else 
				{
				?>
					<option value="<?php echo $key?>"><?php echo $value;?></option>
				<?php	
				}
			} 
			?>
			</select>
			</span></td>
		</tr>
				
		<tr>
			<td class="booked-title">Tour</td>
			<td class="booked-item" colspan="3"><span>
			<?php
			global $wpdb;
			$args_tour = db_query_all_post_in_post_type(-1, 'tour', 'date', 'DESC', $wpdb->prefix.'display', 'display');			
			query_posts($args_tour);			 
			?>
			<select class="select_size" name="update_tour">
			<?php
			
			 while (have_posts())
			 {
			 	the_post();
			 	$postID = get_the_ID();
			 	if($tour[0]->tourID==$postID)
			 	{
			 	?>
			 	<option selected="selected" value="<?php echo $postID;?>"><?php the_title();?></option>
			 	<?php
			 	} 
			 	else 
			 	{
			 	?>
			 	<option value="<?php echo $postID;?>"><?php the_title();?></option>
			 	<?php	
			 	}
			 }
			?>	
			</select>
			</span></td>
		</tr>
		<tr>
			<td class="booked-title">Destination From</td>
			<td class="booked-item"><span><?php echo get_post_meta($tour[0]->tourID, $wpdb->prefix.'destination_from',true);?></span></td>
			
			<td class="booked-title">Destination To</td>
			<td class="booked-item"><span><?php echo get_post_meta($tour[0]->tourID, $wpdb->prefix.'destination_to',true);?></span></td>
		</tr>
		
		<tr>
			<td class="booked-title">Lenght of Day</td>
			<td class="booked-item"><span><?php echo get_post_meta($tour[0]->tourID, $wpdb->prefix.'lenght_of_stay',true);?></span></td>
			
			<td class="booked-title">Propose date of tour</td>
			<td class="booked-item"><span>
										<?php
                                                $originalDate = $tour[0]->proposeDate;
                                                
                                                $newDate_IY = date("Y", strtotime($originalDate));
                                                $newDate_IM = date("m", strtotime($originalDate));
                                                $newDate_ID = date("d", strtotime($originalDate));                                     
                                            ?>
                                            <?php
                                            $date = date('Y');
                                            ?>
                    						<select  name="update_proyr" id="update_proyr">
                    							<?php                    								
                    								for($y = $date; $y <= $date+5 ; $y++)
                    								{
                    									if($y == $newDate_IY)
                    									{
                    									?>
                    										<option selected="selected" value="<?php echo $y;?>"> <?php echo $y;?> </option>
                    									<?php 										
                    									}
                    									else 
                    									{
                    									?>
                    										<option value="<?php echo $y;?>"> <?php echo $y;?> </option>
                    									<?php 	
                    									}					
                    								}				
                    							?>
                    						</select>
                                            
                                            
                    						<select  name="update_promo" id="update_promo">
                    							<option <?php if($newDate_IM == 1) echo 'selected="selected"';?> value="01">January </option>
                                                <option <?php if($newDate_IM == 2) echo 'selected="selected"';?> value="02">February </option>
                                                <option <?php if($newDate_IM == 3) echo 'selected="selected"';?> value="03">March </option>
                                                <option <?php if($newDate_IM == 4) echo 'selected="selected"';?> value="04">April </option>
                                                <option <?php if($newDate_IM == 5) echo 'selected="selected"';?> value="05">May </option>
                                                <option <?php if($newDate_IM == 6) echo 'selected="selected"';?> value="06">June </option>
                                                <option <?php if($newDate_IM == 7) echo 'selected="selected"';?> value="07">July </option>
                                                <option <?php if($newDate_IM == 8) echo 'selected="selected"';?> value="08">August </option>
                                                <option <?php if($newDate_IM == 9) echo 'selected="selected"';?> value="09">September </option>
                                                <option <?php if($newDate_IM == 10) echo 'selected="selected"';?> value="10">October </option>
                                                <option <?php if($newDate_IM == 11) echo 'selected="selected"';?> value="11">November </option>
                                                <option <?php if($newDate_IM == 12) echo 'selected="selected"';?> value="12">December </option>
                    						</select>
                                            
                                            <select  name="update_prody" id="update_prody">
                    							<?php
                    							for($i = 1;$i<=31;$i++)
                    							{
                    								if($i == $newDate_ID)
                    								{
                    									if($i<10)
                    										$j='0'.$i;
                    									else $j=$i
                    								?>
                    									<option selected="selected" value="<?php echo $j;?>"><?php echo $i;?></option>
                    								<?php	
                    								}
                    								else 
                    								{
                    									if($i<10)
                    										$j='0'.$i;
                    									else $j=$i	
	                    							?>
	                    								<option value="<?php echo $j;?>"><?php echo $i;?></option>
	                    							<?php
                    								} 								
                    							}	 
                    							?>												 
                    						</select> 
			</span></td>
		</tr>
		<tr>
			<td class="booked-title">Trevel in party of</td>
			<td class="booked-item"><span>
			<?php
			$travel_list = db_travel_in_party_of();
			if($travel_list)
			{			
			?>
				<select class="select_size" name="update_trevel_in_party">
				<?php						
				 foreach ($travel_list as $key=>$value) 
				 {
				 	$key+=1;
				 	if($key==$tour[0]->travelInpartof)
				 	{
				 	?>
				 	<option selected="selected" value="<?php echo $key;?>"><?php echo $value;?></option>
				 	<?php 	
				 	}
				 	else 
				 	{
				 	?>
				 	<option value="<?php echo $key;?>"><?php echo $value;?></option>
				 	<?php	
				 	}
				 }
				?>
				</select>
			<?php
			} 
			?>	
			</span></td>
			
			<td class="booked-title">Tour Type</td>
			<td class="booked-item"><span>
			<select class="select_size" name="update_tourtype">
			<?php
			$tour_type = db_get_list_tour_type();
			foreach ($tour_type as $key=>$value) 
			{
				if($key==$tour[0]->typeTour)
				{
				?>
				<option selected="selected" value="<?php echo $key;?>"><?php echo $value;?> </option>
				<?php 	
				}
				else 
				{
				?>
				<option value="<?php echo $key;?>"><?php echo $value;?> </option>
				<?php 	
				}		
			} 
			?>	
			</select>
			</span></td>
		</tr>
		
		<tr>
			<td class="booked-title">Tour Price</td>
			<td class="booked-item"><span><?php echo $payment[0]->price;?> USD</span></td>
			
			<td class="booked-title">Deposit</td>
			<td class="booked-item"><span><?php echo $payment[0]->paymentCurrent;?> USD</span></td>
		</tr>
		
		<tr>
			<td class="booked-title"></td>
			<td class="booked-item"><h2>Customer Infomation</h2></td>
		</tr>
		<tr>
			<td class="booked-title">Customer Name</td>
			<td class="booked-item"><span>
			<input type="text" class="select_size" name="update_customerName" value="<?php echo $customers[0]->customerName;?>">
			</span></td>
			
			<td class="booked-title">Customer Address</td>
			<td class="booked-item"><span>
			<input type="text" class="select_size" name="update_customerAddress" value="<?php echo $customers[0]->customerAddress;?>">
			</span></td>
		</tr>
		
		<tr>
			<td class="booked-title">Customer Email</td>
			<td class="booked-item"><span>
			<input type="text" class="select_size" name="update_customerEmail" value="<?php echo $customers[0]->customerEmail;?>">
			</span></td>
			
			<td class="booked-title">Customer Phone</td>
			<td class="booked-item"><span>
			<input type="text" class="select_size" name="update_customerTel" value="<?php echo $customers[0]->customerPhone;?>">
			</span></td>
		</tr>
		<!-- 
		<tr>
			<td class="booked-title">Customer Fax</td>
			<td class="booked-item"><span>
			<input type="text" class="select_size" name="update_customerFax" value="<?php echo $customers[0]->customerFax;?>">
			</span></td>
		</tr>
		 -->
		<tr>
			<td class="booked-title">Customer Comment</td>
			<td class="booked-item"><span><?php echo $customers[0]->customerComment;?></span></td>
		</tr>		
		<tr>
			<td class="booked-title">
			<input type="hidden" name="update_customerID" value="<?php echo $customers[0]->ID;?>">
			</td>
			
			<td class="booked-item">
				<input type="submit" name="update_now_tour" value="Update Now">
			</td>
		</tr>
		<tr>
			<td class="booked-title"></td>
			<td class="booked-item"></td>
		</tr>
	</table>	
</form>		
</div>
